package com.fg.cloud.factory.controller.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.service.agency.AgencyInfoService;
import com.fg.cloud.manager.service.delivery.DeliveryInfoService;
import com.fg.cloud.manager.service.factory.FactoryService;
import com.fg.cloud.manager.service.factory.FactoryStatementService;
import com.fg.cloud.manager.service.factory.TransactionRecordService;
import com.fg.cloud.manager.service.order.OrderInfoService;
import com.fg.cloud.manager.service.order.OrderRefundsService;
import com.fg.cloud.manager.service.product.ProductInfoService;

/**
 * Description:数据重置
 * @author dingjiang
 * @date 2017年11月6日上午10:04:53
 */
@Controller
@RequestMapping("/dataReset")
public class DataResetController extends BaseController{
	
	@Autowired
	private OrderInfoService orderInfoService;
	@Autowired
	private DeliveryInfoService deliveryInfoService;
	@Autowired
	private OrderRefundsService orderRefundsService;
	@Autowired
	private TransactionRecordService tRecordService;
	@Autowired
	private FactoryStatementService statementService;
	@Autowired
	private ProductInfoService productInfoService;
	@Autowired
	private AgencyInfoService agencyInfoService;
	@Autowired
	private FactoryService factoryService;
	
	
	/**
	 * Description:业务数据重置（resetServiceData）
	 * @param req
	 * @param resp
	 * @author dingjiang
	 * @date 2017年11月6日下午4:32:45
	 */
	@RequestMapping(value = "/resetServiceData", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public void resetServiceData(HttpServletRequest req, HttpServletResponse resp){
		try {
			//判断用户是否为管理员
			if ( getCurrentFactoryUser().getIsAdmin() != 1) {
				doResult(resp, false, StatusCode.REMOVEERROR, null, "需要管理员权限！");
				return ;
			}
			Long facId = getCurrentFactoryUser().getFacId();
			//删除订单相关记录（详情、日志、附件）
			boolean flag1 = orderInfoService.deleteOrder(facId);
			if ( !flag1 ) {
				doResult(resp, flag1, StatusCode.REMOVEERROR, null, "订单删除失败");
				return ;
			}
			//删除发货单相关记录（详情、日志、附件）
			boolean flag2 = deliveryInfoService.deleteDelivery(facId);
			if ( !flag2 ) {
				doResult(resp, flag2, StatusCode.REMOVEERROR, null, "发货单删除失败");
				return ;
			}
			//删除退货单相关记录（详情、日志、附件）
			boolean flag3 = orderRefundsService.deleteRefunds(facId);
			if ( !flag3 ) {
				doResult(resp, flag3, StatusCode.REMOVEERROR, null, "退货单删除失败");
				return ;
			}
			//删除交易流水
			boolean flag4 = tRecordService.deleteTrans(facId);
			if ( !flag4 ) {
				doResult(resp, flag4, StatusCode.REMOVEERROR, null, "交易流水删除失败");
				return ;
			}
			//删除对账单（还款明细）
			boolean flag5 = statementService.deleteStatement(facId);
			if ( !flag5 ) {
				doResult(resp, flag5, StatusCode.REMOVEERROR, null, "对账单删除失败");
				return ;
			}
			boolean flag = false;
			if (flag1 && flag2 && flag3 && flag4 && flag5) flag = true;
			
			doResult(resp, flag, flag?StatusCode.REMOVESUCCESS:StatusCode.REMOVEERROR, null, 
					flag?ResultType.REMOVE_SUCCESS:ResultType.REMOVE_FAULT);	
		} catch (Exception e) {
			doResult(resp, false, StatusCode.REMOVEERROR, null, e.getMessage());
			return ;
		}
	}
	
	
	/**
	 * Description:商品数据重置（resetProductData）
	 * @param req
	 * @param resp
	 * @author dingjiang
	 * @date 2017年11月7日上午10:13:53
	 */
	@RequestMapping(value = "/resetProductData", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public void resetProductData(HttpServletRequest req, HttpServletResponse resp){
		try {
			//判断用户是否为管理员
			if ( getCurrentFactoryUser().getIsAdmin() != 1) {
				doResult(resp, false, StatusCode.REMOVEERROR, null, "需要管理员权限！");
				return ;
			}
			Long facId = getCurrentFactoryUser().getFacId();
			boolean flag = productInfoService.deleteProduct(facId);
			doResult(resp, flag, flag?StatusCode.REMOVESUCCESS:StatusCode.REMOVEERROR, null, 
					flag?ResultType.REMOVE_SUCCESS:ResultType.REMOVE_FAULT);
		} catch (Exception e) {
			doResult(resp, false, StatusCode.REMOVEERROR, null, e.getMessage());
			return ;
		}
	}

	
	/**
	 * Description:经销商数据重置
	 * @param req
	 * @param resp
	 * @author dingjiang
	 * @date 2017年11月7日下午1:51:53
	 */
	@RequestMapping(value = "/resetAgencyDate", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public void resetAgencyDate(HttpServletRequest req, HttpServletResponse resp){
		try {
			//判断用户是否为管理员
			if ( getCurrentFactoryUser().getIsAdmin() != 1) {
				doResult(resp, false, StatusCode.REMOVEERROR, null, "需要管理员权限！");
				return ;
			}
			Long facId = getCurrentFactoryUser().getFacId();
			boolean flag = agencyInfoService.deleteAgency(facId);
			doResult(resp, flag, flag?StatusCode.REMOVESUCCESS:StatusCode.REMOVEERROR, null, 
					flag?ResultType.REMOVE_SUCCESS:ResultType.REMOVE_FAULT);
		} catch (Exception e) {
			doResult(resp, false, StatusCode.REMOVEERROR, null, e.getMessage());
			return ;
		}
	}

	/**
	 * Description:初始化数据重置
	 * @param req
	 * @param resp
	 * @author dingjiang
	 * @date 2017年11月7日下午3:55:33
	 */
	@RequestMapping(value = "/resetInitDate", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public void resetInitDate(HttpServletRequest req, HttpServletResponse resp){
		try {
			//判断用户是否为管理员
			if ( getCurrentFactoryUser().getIsAdmin() != 1) {
				doResult(resp, false, StatusCode.REMOVEERROR, null, "需要管理员权限！");
				return ;
			}
			Long facId = getCurrentFactoryUser().getFacId();
			boolean flag = factoryService.deleteInitData(facId);
			doResult(resp, flag, flag?StatusCode.REMOVESUCCESS:StatusCode.REMOVEERROR, null, 
					flag?ResultType.REMOVE_SUCCESS:ResultType.REMOVE_FAULT);
		} catch (Exception e) {
			doResult(resp, false, StatusCode.REMOVEERROR, null, e.getMessage());
			return ;
		}
	}
	

}
